package com.longrise.android.byjk.plugins.aboutme.personalInfo;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.app.BYJKAppLike;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.android.byjk.plugins.aboutme.SecurityActivity;
import com.longrise.android.byjk.plugins.aboutme.personalInfo.change.BundlePhoneActivity;
import com.longrise.android.byjk.plugins.login.ChangePasswordActivity;
import com.longrise.android.byjk.utils.DialogUtil;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.utils.AppUtil;
import com.tencent.mm.opensdk.modelmsg.SendAuth;

/**
 * Created by godliness on 2017/3/7.
 */

public class SecurityPresenter implements PersonalInfoContract.SecurPresenters {

    private static final String TAG = "SecurityPresenter";

    public static final int REQUEST_CODE = 100;

    private PersonalInfoContract.SecurView mSecView;
    private SecurityActivity mCxt;

    public SecurityPresenter(PersonalInfoContract.SecurView view, SecurityActivity act) {
        this.mSecView = view;
        this.mCxt = act;
    }

    @Override
    public void init() {
        UserInfor userInfor = UserInfor.getInstance();
        mSecView.setTitle("账号与安全");
        mSecView.phoneBundle(userInfor.getUserphone());
        String wechartStatus = userInfor.getWenxinstatus();
        mSecView.wechatBundle((wechartStatus == null || wechartStatus.equals("0")) ? "" : userInfor.getWeixinnickname());
        String login = userInfor.getIsopenmobilelogin();
        mSecView.runLoginPhone("", login != null && login.equals("1") ? "已开通" : "未开通");
    }

    @Override
    public void jumpPassword() {
        // mSecView.startActivityForR(REQUEST_CODE, ChangePasswordActivity.class);
        mSecView.toJump(ChangePasswordActivity.class);
    }

    @Override
    public void jumpPhone() {
        //参数传递，代表在账号与安全页进入
        Intent intent = new Intent(mCxt, BundlePhoneActivity.class);
        intent.putExtra(BundlePhoneActivity.EXTRA_IS_FROM, true);
        mSecView.startActivityForR(intent, PersonalPresenter.CHANGEPHONENUMBER);
    }

    @Override
    public void wechatBundle() {
        weChartBundleDialog();
    }

    @Override
    public void jumpRunLogin() {
        mSecView.startActivityForR(PersonalPresenter.RUNLOGIN, OpenLoginActivity.class);
//        Intent intent = new Intent(mCxt, OpenLoginActivity.class);
//        intent.putExtra(OpenLoginActivity.EXTRA_PARAMS, false);
//        mSecView.startActivityForR(intent, PersonalPresenter.RUNLOGIN);
    }

    @Override
    public void close() {
        mSecView.colse();
    }

    @Override
    public void creataDilaog() {
        View v = View.inflate(mCxt, R.layout.dialog_share_success, null);
        TextView textMsg = (TextView) v.findViewById(R.id.dialog_share_success_textview);
        textMsg.setText(mCxt.getString(R.string.wechart_bundle_success));
        creatAlertDialog(mCxt, v, 266, 126);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case REQUEST_CODE:
                if (data != null && data.getBooleanExtra("key", false)) {
                    mSecView.exit();
                }
                break;

            case PersonalPresenter.CHANGEPHONENUMBER:
                if (data != null && data.getBooleanExtra("key", false)) {
                    mSecView.phoneBundle(UserInfor.getInstance().getUserphone());
                }
                break;

            case PersonalPresenter.RUNLOGIN:
                if (data != null && data.getBooleanExtra("key", false)) {
                    mSecView.runLoginPhone(UserInfor.getInstance().getUserphone(), "已开通");
                }
                break;
        }
    }

    private void weChartBundleDialog() {
        View v = View.inflate(mCxt, R.layout.dialog_wechartbundle, null);
        TextView text_Cancle = (TextView) v.findViewById(R.id.text_setcancle);
        TextView text_Confirm = (TextView) v.findViewById(R.id.text_setconfirm);
        TextView hint = (TextView) v.findViewById(R.id.textwehint);
        final String wechartStatus = UserInfor.getInstance().getWenxinstatus();
        hint.setText(wechartStatus == null || wechartStatus.equals("0") ? mCxt.getString(R.string.is_bundle_wechart) : mCxt.getString(R.string.is_unbundle_wechart));
        final View.OnClickListener onClickListener = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (v.getId() == R.id.text_setconfirm) {
                    if (wechartStatus == null || wechartStatus.equals("0")) {   //调用绑定
                        wechartBundle();
                    } else {
                        weChartUnBundle();
                    }
                }
                DialogUtil.getInstance().dismiss();
            }

        };
        text_Cancle.setOnClickListener(onClickListener);
        text_Confirm.setOnClickListener(onClickListener);
        DialogUtil.getInstance().creatAlertDialog(mCxt, v, 266, 171);
    }

    private void wechartBundle() {
        final SendAuth.Req req = new SendAuth.Req();
        req.scope = "snsapi_userinfo";
        req.state = "wechat_sdk_demo_test";
        boolean result = BYJKAppLike.mIWXAPI.sendReq(req);
        if (!result) {
            mCxt.showToast(mCxt.getString(R.string.wechart_bundle_failed));
        }
    }

    private void weChartUnBundle() {
        EntityBean bean = new EntityBean();
        bean.set("userid", UserInfor.getInstance().getUserid());
        bean.set("cardno", UserInfor.getInstance().getUsersfzh());
        EntityBean reqBean = new EntityBean();
        reqBean.set("bean", bean);
        LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl, "bbt_user_uWxUnbind", reqBean, new LoadDataManager.OnRequestCallListener() {
            @Override
            public void onSuccess(String key, String service, Object result) {
                mSecView.wechatBundle(mCxt.getString(R.string.none_bundle));
                UserInfor.getInstance().setWenxinstatus("0");
                View v = View.inflate(mCxt, R.layout.dialog_share_success, null);
                TextView textMsg = (TextView) v.findViewById(R.id.dialog_share_success_textview);
                textMsg.setText(mCxt.getString(R.string.wechart_unbundle_success));
                creatAlertDialog(mCxt, v, 266, 126);
            }

            @Override
            public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
                mCxt.showToast(mCxt.getString(R.string.networkerr));
            }

            @Override
            public void onFinished(String key, String service) {
            }
        });
    }

    private void creatAlertDialog(Context context, View view, int width, int height) {
        Dialog mDialog = new Dialog(context);
        mDialog.show();
        width = AppUtil.dip2px(width);
        height = AppUtil.dip2px(height + 15 / 4);
        Window dialogWindow = mDialog.getWindow();
        dialogWindow.setContentView(view);
        dialogWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.CENTER_HORIZONTAL);
        lp.width = width; // 宽度
        lp.height = height * 4 / 3; // 高度
        dialogWindow.setAttributes(lp);
        mDialog.setCanceledOnTouchOutside(true);
    }
}
